package com.shzu.jwc.ty.controller;

import java.util.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shzu.jwc.ty.bean.TheoreticalExaminationAppointment;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shzu.jwc.ty.common.ExportExcelUtils;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.Semester;

@Controller
@RequestMapping("/konwledgescore")
public class KnowledgeScorecontroller {

	@Autowired
	StudentInfoservice studentInfoservice;
	
	@Autowired
	JwcKnowledgeScoreservice knowledgeScoreservice;
	
	@Autowired
	SystemConfigservice systemConfigservice;
	
	@Autowired
	TheoreticalExaminationservice theoreticalExaminationservice;

	@Autowired
	SystemYearservice yearservice;
	
	@RequestMapping(value = "/main.action",method = RequestMethod.GET)
	public String showmain(Model model){
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("dates",yearservice.getSystemYear());
		model.addAttribute("code", systemConfigservice.getvalue("sportsknowledge"));
		model.addAttribute("name", theoreticalExaminationservice.getkowlegetest());
		return "konwledgescore/main";
	}

	@ResponseBody
	@RequestMapping(value = "/getData.action",method = RequestMethod.POST)
	public Map<String,Object> getData(@RequestParam("systemYearId") Integer systemYearId,@RequestParam("theoreticalExaminationAppointmentScore") Double theoreticalExaminationAppointmentScore,@RequestParam("theoreticalExaminationId") Integer theoreticalExaminationId,@RequestParam("userStudentInfoNum") String userStudentInfoNum){
		if(systemYearId==0){
			systemYearId=yearservice.getoneyearby(Semester.getnow());
		}
		if(userStudentInfoNum==""){
            userStudentInfoNum=null;
        }
		List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.getData(systemYearId,theoreticalExaminationAppointmentScore,theoreticalExaminationId,userStudentInfoNum);
		Map<String,Object> maps=new HashMap<String,Object>();
		maps.put("data",data);
		return maps;
	}

	@ResponseBody
	@RequestMapping(value = "/updateMultScore.action",method = RequestMethod.POST)
	public Msg updateMultScore(@RequestParam("name") String name,@RequestParam("score") Double score,@RequestParam("theoreticalExaminationAppointmentId") Integer theoreticalExaminationAppointmentId){
		try{
			Double Totalscore=knowledgeScoreservice.updateMultscore(name,score,theoreticalExaminationAppointmentId);
			return Msg.success("操作成功").add("Totalscore",Totalscore);
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}

	}

    @RequestMapping("/newExport.action")
    public ModelAndView newExport(@RequestParam("systemYearId") Integer systemYearId,@RequestParam("theoreticalExaminationAppointmentScore") Double theoreticalExaminationAppointmentScore,@RequestParam("theoreticalExaminationId") Integer theoreticalExaminationId,@RequestParam("userStudentInfoNum") String userStudentInfoNum,HttpServletResponse response) {

        if(systemYearId==0){
            systemYearId=yearservice.getoneyearby(Semester.getnow());
        }
        if(userStudentInfoNum==""){
            userStudentInfoNum=null;
        }
        try {
            List<TheoreticalExaminationAppointment> data = knowledgeScoreservice.getData(systemYearId, theoreticalExaminationAppointmentScore, theoreticalExaminationId, userStudentInfoNum);
            String code = systemConfigservice.getvalue("sportsknowledge");
            String title = "体育知识考试成绩";
            String[] rowsName = new String[]{"学年", "考试名称", "学号", "姓名", "入学时间", "所在学院", "课程", "课程代码", "考试成绩", "平时成绩", "期中成绩", "总成绩"};
            List<Object[]> dataList = new ArrayList<Object[]>();
            Object[] objs = null;
            for (int i = 0; i < data.size(); i++) {
                TheoreticalExaminationAppointment po = null;
                po = data.get(i);
                objs = new Object[rowsName.length];
                objs[0] = po.getSystemYear().getSystemYearName();
                objs[1] = po.getTheoreticalExamination().getTheoreticalExaminationName();
                objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
                objs[3] = po.getUserStudentInfo().getUserStudentInfoName();
                objs[4] = po.getUserStudentInfo().getUserStudentInfoYear();
                objs[5] = po.getUserStudentInfo().getUserStudentInfoCollege();
                objs[6] = "体育知识考试";
                objs[7] = code;
                objs[8] = po.getTheoreticalExaminationAppointmentScore() == null ? "" : po.getTheoreticalExaminationAppointmentScore();
                objs[9] = po.getTheoreticalExaminationNormalscore() == null ? "" : po.getTheoreticalExaminationNormalscore();
                objs[10] = po.getTheoreticalExaminationMidscore() == null ? "" : po.getTheoreticalExaminationMidscore();
                objs[11] = po.getTheoreticalExaminationTotalscore() == null ? "" : po.getTheoreticalExaminationTotalscore();
                dataList.add(objs);
            }
            ExportExcelUtils ex = new ExportExcelUtils(title, rowsName, dataList, response);
            ex.exportData();
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/newExportJwc.action")
    public ModelAndView newExportJwc(@RequestParam("systemYearId") Integer systemYearId,@RequestParam("theoreticalExaminationAppointmentScore") Double theoreticalExaminationAppointmentScore,@RequestParam("theoreticalExaminationId") Integer theoreticalExaminationId,@RequestParam("userStudentInfoNum") String userStudentInfoNum,HttpServletResponse response) {

        if(systemYearId==0){
            systemYearId=yearservice.getoneyearby(Semester.getnow());
        }
        if(userStudentInfoNum==""){
            userStudentInfoNum=null;
        }
        try {
            List<TheoreticalExaminationAppointment> data = knowledgeScoreservice.getData(systemYearId, theoreticalExaminationAppointmentScore, theoreticalExaminationId, userStudentInfoNum);
            String code=systemConfigservice.getvalue("sportsknowledge");
            String title="体育知识考试成绩";
            String[] rowsName=new String[] {"取得学年","取得学期","学号","课程／环节代码","课程类别一","课程类别二","课程类别三","环节类别","修读性质","考核方式","辅修标记","补考标记","特殊情况标记","成绩表现形式","原始成绩","授课方式","等级","平时成绩","平时成绩表现形式","平时成绩百分比","期中成绩","期中成绩表现形式","期中成绩百分比","期末成绩","期末成绩表现形式","期末成绩百分比"};
            List<Object[]> dataList =new ArrayList<Object[]>();
            Object[] objs=null;
            String percentageNormal = systemConfigservice.getvalue("theo_examination_normal")+"%";
            String percentageMid = systemConfigservice.getvalue("theo_examination_mid")+"%";
            String percentageScore = systemConfigservice.getvalue("theo_examination_end")+"%";
            for (int i = 0; i < data.size(); i++) {
                TheoreticalExaminationAppointment po=null;
                po=data.get(i);
                objs = new Object[rowsName.length];
                String[] xq=Semester.getnow().split("-");
                objs[0] = xq[0];
                objs[1] = Integer.parseInt(xq[2])==1?"第一学期":"第二学期";
                objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
                objs[3] = code;
                objs[4] = "必修课";
                objs[5]="";
                objs[6]="";
                objs[7]="";
                objs[8] = "初修";
                objs[9] = "考试";
                if(po.getTheoreticalExaminationAppointmentScore()==null)
                    objs[12] = "缺考";
                else
                    objs[12]=null;
                objs[14] = po.getTheoreticalExaminationTotalscore();
                objs[17] = po.getTheoreticalExaminationNormalscore();
                objs[19] = percentageNormal;
                objs[20] = po.getTheoreticalExaminationMidscore();
                objs[22] = percentageMid;
                objs[23] = po.getTheoreticalExaminationAppointmentScore();
                objs[25] = percentageScore;
                dataList.add(objs);
            }

            ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);
            ex.exportData();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }


	@RequestMapping("/selectkonwledgescore.action")
	public String selectkonwledgescore(@RequestParam("theoreticalExaminationId") Integer theoreticalExaminationId,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model,HttpSession session) {
		if(theoreticalExaminationId==0) {
			Page page=new Page(knowledgeScoreservice.countknowledgescore(), pn);
			page.setPageSize(20);
			List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.getknowledgescore(page);
			model.addAttribute("data", data);
			model.addAttribute("page", page);
		}else
		{
			Page page=new Page(knowledgeScoreservice.countselectknowledgescore(theoreticalExaminationId), pn);
			page.setPageSize(20);
			List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.selectknowledgescore(theoreticalExaminationId,page);
			model.addAttribute("data", data);
			model.addAttribute("page", page);
		}
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("code", systemConfigservice.getvalue("sportsknowledge"));
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("name", theoreticalExaminationservice.getkowlegetest());
		session.setAttribute("theoreticalExaminationId", theoreticalExaminationId);
		return "konwledgescore/konwledgescore";
	}

	@RequestMapping("/konwledgescore.action")
	public String getkonwledgescore(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		Page page=new Page(knowledgeScoreservice.countknowledgescore(), pn);
		page.setPageSize(20);
		List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.getknowledgescore(page);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsknowledge"));
		model.addAttribute("name", theoreticalExaminationservice.getkowlegetest());
		model.addAttribute("page", page);
		return "konwledgescore/konwledgescore";
	}

	@RequestMapping("/searchkonwledgescore.action")
	public String searchkonwledgescore(@RequestParam("userStudentInfoNum")String userStudentInfoNum,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
		List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.searchknowledgescore(userStudentInfoNum);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("code", systemConfigservice.getvalue("sportsknowledge"));
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("name", theoreticalExaminationservice.getkowlegetest());
		return "konwledgescore/konwledgescore";
	}

	@ResponseBody
	@RequestMapping("/updatescore.action")
	public Msg updatescore(@RequestParam("theoreticalExaminationAppointmentScore")double theoreticalExaminationAppointmentScore,@RequestParam("theoreticalExaminationAppointmentId")Integer theoreticalExaminationAppointmentId){
		try {
			knowledgeScoreservice.updatescore(theoreticalExaminationAppointmentScore,theoreticalExaminationAppointmentId);
			return Msg.success("操作成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}
	}
	
	@RequestMapping("/export.action")
	public ModelAndView export(@RequestParam("userStudentInfoYear") Integer userStudentInfoYear,@RequestParam("userStudentInfoCollege") String userStudentInfoCollege,@RequestParam("theoreticalExaminationId")Integer theoreticalExaminationId,HttpServletResponse response) {
		try {
			List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.upselectknowledgescore(userStudentInfoYear,userStudentInfoCollege,theoreticalExaminationId);
			String code=systemConfigservice.getvalue("sportsknowledge");
			TheoreticalExaminationAppointment appointment=new TheoreticalExaminationAppointment();
			String title="体育知识考试成绩";
			String[] rowsName=new String[] {"取得学年","取得学期","学号","课程／环节代码","课程类别一","课程类别二","课程类别三","环节类别","修读性质","考核方式","辅修标记","补考标记","特殊情况标记","成绩表现形式","原始成绩"};
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;
			
			for (int i = 0; i < data.size(); i++) {  
				TheoreticalExaminationAppointment po=null;
						po=data.get(i);
                objs = new Object[rowsName.length];  
                String[] xq=Semester.getnow().split("-");
                objs[0] = xq[0];
                objs[1] = Integer.parseInt(xq[2])==1?"第一学期":"第二学期";
                objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
                objs[3] = code;
                objs[4] = "必修课";
				objs[5]="";
				objs[6]="";
				objs[7]="";
                objs[8] = "初修";  
                objs[9] = "考试";
				if(po.getTheoreticalExaminationAppointmentScore()==null)
					objs[12] = "缺考";
				else
					objs[12]=null;
                objs[14] = po.getTheoreticalExaminationAppointmentScore();
                dataList.add(objs);  
            }  
			
            ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);  
            ex.exportData();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}


}
